//发送验证码
Vue.component('sendcode', {
    props: ['type', 'phone'],
    template: '<a @click="sendCode" :style="[baseStyle,activeStyle]">{{sentTime}}{{tips}}</a>',
    data: function () {
        return {
            sent: false,
            hasClicked: false,
            sentTime: '',
            tips: '发送验证码',
            int: null,
            baseStyle: {
                position: 'absolute',
                height: '28px',
                lineHeight: '28px',
                top: '50%',
                marginTop: '-14px',
                padding: '0 10px',
                color: '#',
                borderRadius: '15px',
                fontSize: '12px',
            }
        }
    },
    computed: {
        activeStyle: function () {
            return {
                // backgroundColor: this.sent ? '#cccccc' : '#e5001e'
            }
        },
    },
    watch: {
        sentTime: function (n) {
            if (n > 0) {
                return;
            } else {
                this.sent = false;
                this.sentTime = '';
                this.tips = '发送验证码';
                clearInterval(this.int);
            }
        }
    },
    methods: {
        sendCode: function () {
            if (this.sent || this.hasClicked) {
                return;
            }
            this.hasClicked = true;

            var data = {
                phone: this.phone,
                type: this.type,
                timestamp: (Date.parse(new Date())) / 1000,
            };

            var str = pinet.formatData(data);
            var sign = pinet.md5(str);

            data['sign'] = sign;

            var that = this;
            var callback = function () {
                that.hasClicked = false;
                that.sent = true;
                that.changeTips();
            };
            var errback = function (ret) {
                that.hasClicked = false;
                if (ret && ret.message) {
                    alert(ret.message);
                }
            };
            pinet.post(URL.sendCode, data, callback, errback);
        },
        changeTips: function () {
            this.sentTime = 60;
            this.tips = 's后重发';

            var that = this;

            this.int = setInterval(function () {
                that.sentTime--;
            }, 1000);
        }
    }
});

//点击获取邀请码
Vue.component('getInvite_code', {
    template: "<div style='text-align:center;'><span @click='getInviteCode' style='padding:0 15px;border:1px solid #eee;border-radius:10px;height:30px;line-height:30px;background-color:#f09401;color:white;'>没有邀请码点击获取</span></div>",
    methods: {
        getInviteCode: function () {
            var that = this;
            var callback = function (ret) {
                that.$emit('send-invite_code', ret.content);
            };
            pinet.post(URL.getInviteCode, {}, callback);
        }
    },
});

Vue.component('lazy-load', {
    props: ['countPerPage', 'arr', 'url', 'param', 'retAttr'],
    template: "<div style='font-size:14px;text-align:center;margin-bottom:10px;'>{{hasMore ? '正在加载更多...' : '已到底部啦'}}</div>",
    data: function () {
        return {
            page: 1,
            busy: false,
            noMore: false,
        }
    },
    computed: {
        countOfLastPage: function () {
            return this.arr.length % this.countPerPage;

        },
        hasMore: function () {
            return this.countOfLastPage == 0 && !this.noMore;
        },
    },
    watch: {
        arr: {
            handler: function (n) {
                if (n.length <= this.countPerPage) {
                    this.page = 1
                }
            },
            deep: true,
        }
    },
    methods: {
        loadMore: function () {
            if (!this.hasMore) {
                return;
            }
            this.busy = true;

            var data = {
                page: this.page + 1,
                pageSize: this.countPerPage,
                member_id: getPrefs('member_id')
            };

            if (this.param) {
                concatJson(data, this.param);
            }

            var that = this;
            var callback = function (ret) {
                var more = that.retAttr ? ret.content[that.retAttr] : ret.content;
                if (more.length > 0) {
                    that.arr.push.apply(that.arr, more);
                    that.page++;
                } else {
                    that.noMore = true;
                }
                that.busy = false;
            };
            var errback = function () {
                that.busy = false
            };
            pinet.post(this.url, data, callback, errback);
        },
        doScroll: function () {
            if (document.body.scrollTop + window.innerHeight >= document.body.scrollHeight) {
                if (!this.busy) {
                    this.loadMore();
                }
            }
        },
    },
    mounted: function () {
        window.addEventListener('scroll', this.doScroll);
    }

});

Vue.component('alilazy-load', {
    props: ['countPerPage', 'arr', 'url', 'otherDatas', 'retAttr'],
    template: "<div style='font-size:14px;text-align:center;margin-bottom:10px;' id='loadmore'>{{hasMore ? '正在加载更多...' : '已到底部啦'}}</div>",
    data: function () {
        return {
            page: 1,
            busy: false,
            noMore: false,
        }
    },
    computed: {
        countOfLastPage: function () {
          if(this.arr.length==0){
            return 0 ;
          }else {
          return this.arr.length % this.countPerPage;
          }
        },
        hasMore: function () {
            return this.countOfLastPage == 0 && !this.noMore;
        },
    },
    watch: {
        arr: {
            handler: function (n) {
                if (n.length <= this.countPerPage) {
                    this.page = 1
                }
            },
            deep: true,
        }
    },
    methods: {
        loadMore: function () {
            if (!this.hasMore) {
                return;
            }
            this.busy = true;

            var data = {
                page: this.page + 1,
                pageSize: this.countPerPage,
                member_id: getPrefs('member_id')
            };

            if (this.otherDatas) {
                concatJson(data, this.otherDatas);
            }

            var that = this;
            pinet.get(this.url+'&toPage='+(this.page + 1)+'&perPageSize='+this.countPerPage, function (result) {
                var more = result.data.pageList;
                if (more.length > 0) {
                    for(var i=0;i<more.length;i++){
                      more[i].select = '-1';
                    }
                    that.arr.push.apply(that.arr, more);
                    that.page++;
                } else {
                    that.noMore = true;
                }
                that.busy = false;
            });
        },
        doScroll: function () {
            if (document.body.scrollTop + window.innerHeight >= document.body.scrollHeight) {
                if (!this.busy) {
                  // document.getElementById('search-container').style.display = 'none';
                    this.loadMore();
                }
            }
        },
    },
    mounted: function () {
        window.addEventListener('scroll', this.doScroll);
    }

});
